package com.android.lovepick.util;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.lovepick.R;

public class ChatMsgAdapter extends BaseAdapter
{
	private List<ChatMsg> mMsgList = null;
	private Context mContext;
	private LayoutInflater mInflater;

	public ChatMsgAdapter(Context context, List<ChatMsg> list)
	{
		this.mMsgList = list;
		this.mContext = context;
		mInflater = LayoutInflater.from(mContext);
	}

	public int getCount()
	{
		// TODO Auto-generated method stub
		return mMsgList.size();
	}

	public Object getItem(int position)
	{
		// TODO Auto-generated method stub
		return mMsgList.get(position);
	}

	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		ChatMsg chatMsg = mMsgList.get(position);
		boolean sendOrRcv = chatMsg.isSendOrRcv();

		if (convertView == null)
		{
			if (sendOrRcv)
			{
				// 消息是发送的
				convertView = mInflater.inflate(R.layout.chatmsg_item_send,
						null);
			}
			else
			{
				convertView = mInflater
						.inflate(R.layout.chatmsg_item_rcv, null);
			}

			viewHolder = new ViewHolder();
			viewHolder.txvDate = (TextView) convertView
					.findViewById(R.id.txv_date);
			viewHolder.txvContent = (TextView) convertView
					.findViewById(R.id.txv_content);
			viewHolder.sendOrRcv = sendOrRcv;
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.txvDate.setText(chatMsg.getDate());
		viewHolder.txvContent.setText(chatMsg.getContent());

		return convertView;
	}

	static class ViewHolder
	{
		public TextView txvDate;
		public TextView txvContent;
		public boolean sendOrRcv;
	}

}
